package org.cid54.jshopper.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 *
 * @author fgo
 */
@Entity
@Table(uniqueConstraints = {
    @UniqueConstraint(columnNames = {"shoppingList", "item"})})
public class ShopItem extends SuperEntity {

    @ManyToOne(optional = false)
    @JoinColumn(name = "shoppingList")
    private ShoppingList shoppingList;
    @ManyToOne(optional = false)
    @JoinColumn(name = "item")
    private Item item;
    @Column(nullable = false, length = 4)
    private int amount;
    @Column(nullable = false)
    private boolean bought = false;

    public boolean isBought() {
        return bought;
    }

    public void setBought(boolean bought) {
        this.bought = bought;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public ShoppingList getShoppingList() {
        return shoppingList;
    }

    public void setShoppingList(ShoppingList shoppingList) {
        this.shoppingList = shoppingList;
        shoppingList.getShopItems().add(this);
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ShopItem other = (ShopItem) obj;
        if (this.shoppingList != other.shoppingList && (this.shoppingList == null || !this.shoppingList.equals(other.shoppingList))) {
            return false;
        }
        if (this.item != other.item && (this.item == null || !this.item.equals(other.item))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 53 * hash + (this.shoppingList != null ? this.shoppingList.hashCode() : 0);
        hash = 53 * hash + (this.item != null ? this.item.hashCode() : 0);
        return hash;
    }
}
